// pages/medical/reservation/index.js
import Dialog from '@vant/weapp/dialog/dialog';
import {
  createReservation,
  checkUserIsExist
} from "../../../utils/hospital-api";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    options: {},
    hospital: {},
    dept: {},
    doctor: {},
    schedule: {},
    cardNo: '',
    cardPassword: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      options: options
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    this.setData({
      hospital: wx.getStorageSync('hospital'),
      dept: wx.getStorageSync('dept'),
      doctor: wx.getStorageSync('doctor'),
      schedule: wx.getStorageSync('schedule'),
      scheduleTime: wx.getStorageSync('scheduleTime'),
      user: wx.getStorageSync('userInfo')
    })
  },
  // 预约操作
  hanldeReservation() {
    checkUserIsExist().then(res => {
      let params = {
        idNum: this.data.user.idNumber,
        idType: '0',
        schCode: this.data.schedule.SCHCODE,
        priIdNum: this.data.user.idNumber,
        path: 'wap',
      }
      if (this.data.hospital.ISSEG == 1) {
        params.secCode = this.data.scheduleTime.SECCODE
      }
      if (this.data.hospital.HOSFLAG == 1) {
        params.cardNo = this.data.cardNo
        params.cardPassword = this.data.cardPassword
      }
      wx.showLoading({
        title: '预约中',
      })
      createReservation({
        data: JSON.stringify({
          idNum: this.data.user.idNumber,
          idType: '0',
          schCode: this.data.schedule.SCHCODE,
          secCode: this.data.scheduleTime.SECCODE,
          priIdNum: this.data.user.idNumber,
        })
      }).then(res => {
        wx.hideLoading();
        if (res.data.SUCCESSFUL == 1) {
          wx.showModal({
            title: '提示',
            content: '预约成功',
            showCancel: false,
            success(res) {
              wx.navigateBack()
            }
          })
        } else {
          wx.showModal({
            title: '预约失败',
            content: res.data.MSG,
            showCancel: false,
            success(res) {
              wx.navigateBack()
            }
          })
        }
      })
    })

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})